package com.zst.dollars.handler.client;

import com.zst.dollars.common.Consts;
import com.zst.dollars.entity.connection.ServerConnectResultMessage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Semaphore;

/**
 * 客户端接受服务端发过来的token的Handler
 */
public class ConnectionActiveHandler extends SimpleChannelInboundHandler<ServerConnectResultMessage> {
    private static final Logger logger = LoggerFactory.getLogger(ConnectionActiveHandler.class);

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ServerConnectResultMessage msg) throws Exception {
        ctx.channel().attr(Consts.tokenKey).set(msg.getClientToken());
        ctx.channel().attr(Consts.smeaphoreKey).set(new Semaphore(0));
        logger.debug("从服务端获取到ClientToken:{}", msg.getClientToken());
    }
}
